package com.zx.tradeclient.ctrl.index5;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zx.base.ctrl.BaseActivity;
import com.zx.base.http.HttpUtils;
import com.zx.base.http.HttpUtils.CallBack;
import com.zx.base.widget.CommentDialog;
import com.zx.base.widget.ExpanderDialog;
import com.zx.base.widget.ExpanderDialog.DialogListener;
import com.zx.tradeclient.R;
import com.zx.tradeclient.app.TradeApp;

public class WantEditActivity extends BaseActivity {

	private Map<String, String> map = new HashMap<String, String>(); // 签到和抽奖时向服务器发送的参数
	// private String result = ""; // 服务器返回的数据

	private Button good_info_need;
	private String title, value, type;;
	private TradeApp app;
	private ExpanderDialog expander;
	private int NeworEdit = 0;
	private String wantid = "";
	private CommentDialog comment;
	private ProgressDialog progressDialog;
	private TextView want_info_text_productTypeMin, want_info_text_cityId, want_info_text_askContent, want_info_text_packAsk;
	private EditText want_info_edit_productName, want_info_edit_unit, want_info_edit_num, want_info_edit_price, want_info_edit_linkMan,
			want_info_edit_linkMobile, want_info_edit_linkTelphone, want_info_edit_email, want_info_edit_address;
	private LinearLayout want_info_line_productTypeMin, want_info_line_cityId, want_info_line_packAsk, want_info_line_askContent;
	private String productTypeMinId="", cityId="";

	private void init() {
		try {
			if (getIntent().getStringExtra("act").equals("new")) {

			} else if (getIntent().getStringExtra("act").equals("edit")) {
				NeworEdit = 1;
				wantid = getIntent().getStringExtra("wantid");
			}
		} catch (Exception e) {
			e.printStackTrace();
			NeworEdit = 0;

		}

		want_info_text_productTypeMin = (TextView) findViewById(R.id.want_info_text_productTypeMin);
		want_info_text_cityId = (TextView) findViewById(R.id.want_info_text_cityId);
		want_info_text_askContent = (TextView) findViewById(R.id.want_info_text_askContent);
		want_info_text_packAsk = (TextView) findViewById(R.id.want_info_text_packAsk);

		want_info_edit_productName = (EditText) findViewById(R.id.want_info_edit_productName);
		want_info_edit_unit = (EditText) findViewById(R.id.want_info_edit_unit);
		want_info_edit_num = (EditText) findViewById(R.id.want_info_edit_num);
		want_info_edit_price = (EditText) findViewById(R.id.want_info_edit_price);
		want_info_edit_linkMan = (EditText) findViewById(R.id.want_info_edit_linkMan);
		want_info_edit_linkMobile = (EditText) findViewById(R.id.want_info_edit_linkMobile);
		want_info_edit_linkTelphone = (EditText) findViewById(R.id.want_info_edit_linkTelphone);
		want_info_edit_email = (EditText) findViewById(R.id.want_info_edit_email);
		want_info_edit_address = (EditText) findViewById(R.id.want_info_edit_address);

		want_info_line_productTypeMin = (LinearLayout) findViewById(R.id.want_info_line_productTypeMin);
		want_info_line_cityId = (LinearLayout) findViewById(R.id.want_info_line_cityId);
		want_info_line_packAsk = (LinearLayout) findViewById(R.id.want_info_line_packAsk);
		want_info_line_askContent = (LinearLayout) findViewById(R.id.want_info_line_askContent);
		want_info_line_productTypeMin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				expander = new ExpanderDialog(WantEditActivity.this, new DialogListener() {

					@Override
					public void endme(String group, String value, int groupid, int valueid) {
						//
						want_info_text_productTypeMin.setText(group + " " + value);
						productTypeMinId = app.typeids[groupid][valueid];
					}
				}, 2);
				expander.show();

			}
		});
		want_info_line_packAsk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//
				comment = new CommentDialog(WantEditActivity.this, "外观要求", "提交", "取消", false);
				comment.show();
				comment.bt_yes.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// 提交评论
						String poll_con = comment.editor.getText().toString().trim();
						want_info_text_packAsk.setText(poll_con);
						comment.dlg.dismiss();
					}
				});
				comment.bt_no.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// 取消
						comment.dlg.dismiss();
					}
				});

			}
		});
		want_info_line_askContent.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				//
				comment = new CommentDialog(WantEditActivity.this, "求购要求", "提交", "取消", false);
				comment.show();
				comment.bt_yes.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// 提交评论
						String poll_con = comment.editor.getText().toString().trim();
						want_info_text_askContent.setText(poll_con);
						comment.dlg.dismiss();
					}
				});
				comment.bt_no.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// 取消
						comment.dlg.dismiss();
					}
				});

			}
		});
		want_info_line_cityId.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				expander = new ExpanderDialog(WantEditActivity.this, new DialogListener() {

					@Override
					public void endme(String group, String value, int groupid, int valueid) {
						//
						want_info_text_cityId.setText(group + " " + value);
						cityId = app.cityids[groupid][valueid];
					}
				}, 1);
				expander.show();

			}
		});

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.want_edit_info);
		init();
		setHeader(1);
		app = (TradeApp) getMyApplication();
		this.header_right.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 提交
				progressDialog = ProgressDialog.show(WantEditActivity.this, null, "正在提交数据，请稍后.....", true, true);
				editWantInfoTask();
			}
		});
		if (NeworEdit == 1) {
			progressDialog = ProgressDialog.show(WantEditActivity.this, null, "正在接收数据，请稍后.....", true, true);
			readWantInfoTask(wantid);
		}
	}

	private void editWantInfoTask() {

		Map<String, String> map = new HashMap<String, String>();
		map.put("id", wantid);
		map.put("act", "demandEdit");
		map.put("userId", app.user.getId());
		map.put("title", want_info_edit_productName.getText().toString().trim());
		map.put("price", want_info_edit_price.getText().toString().trim());
		map.put("num", want_info_edit_num.getText().toString().trim());
		map.put("unit", want_info_edit_unit.getText().toString().trim());
		map.put("askContent", want_info_text_askContent.getText().toString().trim());
		map.put("packAsk", want_info_text_packAsk.getText().toString().trim());
		map.put("linkMan", want_info_edit_linkMan.getText().toString().trim());
		map.put("linkMobile", want_info_edit_linkMobile.getText().toString().trim());
		map.put("linkTelphone", want_info_edit_linkTelphone.getText().toString().trim());
		map.put("email", want_info_edit_email.getText().toString().trim());
		map.put("address", want_info_edit_address.getText().toString().trim());
		map.put("productTypeMin", productTypeMinId);
		map.put("cityId", cityId);
		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {

				try {
					String succeed = response.getString("succeed");
					if(succeed.equals("000")){
						Toast toast = Toast.makeText(WantEditActivity.this, "提交成功。", Toast.LENGTH_SHORT);
						toast.setGravity(Gravity.CENTER, 0, 0);
						toast.show();
					}
					goback();
				} catch (JSONException e) {
					e.printStackTrace();
				} finally {
					if (progressDialog != null)
						progressDialog.dismiss();
					progressDialog = null;
				}

			}

			@Override
			public void onFailure(Throwable e) {
				Toast toast = Toast.makeText(WantEditActivity.this, "网络超时。", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();

			}
		});
	}

	private void readWantInfoTask(String wantId) {

		Map<String, String> map = new HashMap<String, String>();
		map.put("act", "demandDetail");
		map.put("id", wantId);
		HttpUtils.post(map, new CallBack() {

			@Override
			public void onSuccess(JSONObject response) {
				parseJson(response);

			}

			@Override
			public void onFailure(Throwable e) {
				Toast toast = Toast.makeText(WantEditActivity.this, "网络超时。", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();

			}
		});
	}

	private void parseJson(JSONObject response) {
		try {
			String succeed = response.getString("succeed");
			if (succeed.equals("000")) {
				cityId = response.getString("cityId");
				productTypeMinId = response.getString("productTypeMinId");
				want_info_edit_productName.setText(response.getString("title"));
				want_info_edit_unit.setText(response.getString("unit"));
				want_info_edit_num.setText(response.getString("num"));
				want_info_edit_price.setText(response.getString("price"));
				want_info_edit_linkMan.setText(response.getString("linkMan"));
				want_info_edit_linkMobile.setText(response.getString("linkMobile"));
				want_info_edit_linkTelphone.setText(response.getString("linkTelphone"));
				want_info_edit_email.setText(response.getString("email"));
				want_info_edit_address.setText(response.getString("address"));
				want_info_text_productTypeMin.setText(response.getString("title"));
				want_info_text_cityId.setText(response.getString("province")+response.getString("city"));
				want_info_text_askContent.setText(response.getString("askContent"));
				want_info_text_packAsk.setText(response.getString("packAsk"));
			} else if (succeed.equals("002")) {
				Toast toast = Toast.makeText(WantEditActivity.this, "没有数据。", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			} else {
				Toast toast = Toast.makeText(WantEditActivity.this, "后台出错,请联系系统管理员。", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} finally {
			if (progressDialog != null)
				progressDialog.dismiss();
			progressDialog = null;
		}
	}
}
